package com.csun.cmny.provider.device.watch.entity;

import java.text.DecimalFormat;

/// <summary>
    ///  上行开机数据包
    ///  关联下行 确认 004
    ///  Uplink boot data package
    ///  Association confirmation command FunCode: 004
    /// </summary>
    public class C003
    {
        /// <summary>
        /// 数据起封包 @B#@ 
        /// Start Fixed value :@B#@ 
        /// </summary>
        public String HeadCode = "@B#@";
        /// <summary>
        /// 协议版本号
        /// Protocol version number
        /// </summary>
        public String Protocol =null;

        /// <summary>
        /// 功能代码识别
        /// Function code
        /// </summary>
        public final String FunCode = "003";
        /// <summary>
        /// 国际移动设备身份码
        /// International Mobile Equipment Identity
        /// </summary>
        public String IMEI  = null;
        /// <summary>
        /// 国际移动用户识别码
        /// International Mobile Subscriber Identification Number
        /// </summary>
        public String IMSI = null;
        /// <summary>
        /// 终端版本
        /// Terminal version
        /// </summary>
        public String WearVersion  =null;
        /// <summary>
        /// 佩戴状态 （0:未佩戴 ;1:已佩戴）
        /// Terminal wear state  (0: is not worn; 1: is worn)
        /// </summary>
        public int WearState  = 0;
        /// <summary>
        /// 剩余电量百分比（range:1-100）
        /// Percentage of residual electricity (range:1-100)
        /// </summary>
        public int Power   = 0;
        /// <summary>
        /// 终端当前时间(年月日时分秒) yyyyMMddHHmmss
        /// The terminal current time ( format:yyyyMMddHHmmss)
        /// </summary>
        public String TerminalTime = null;
        /// <summary>
        /// 当LbsType(地理位置类型)为 1 (GPS)  手环上传格式：纬度值|经度值
        /// When LbsType (geographical location type) was 1 (GPS) format:   latitude|longitude
        /// </summary>
        public String GpsString = null;
        /// <summary>
        /// 当LbsType(地理位置类型)为 2 (基站)  手环上传格式：cell_id,lac,mnc,mcc,rssi|cell_id,lac,mnc,mcc,rssi|...
        /// When LbsType (geographic location type) was 2 (cell),  upload format: cell_id,lac,mnc,mcc,rssi|cell_id,lac,mnc,mcc,rssi|...
        /// </summary>
        public String CellString = null;
        /// <summary>
        /// 当LbsType(地理位置类型)为 3 (WIFI+基站)  手环上传格式：1c:fa:56:b5,-61&1c:fa:56:b5,-61|cell_id,lac,mnc,mcc,rss&cell_id,lac,mnc,mcc,rss
        /// When LbsType (geographic location type) was 2 (wifi+cell),  upload format:  1c:fa:56:b5,-61&1c:fa:56:b5,-61|cell_id,lac,mnc,mcc,rss&cell_id,lac,mnc,mcc,rss
        /// </summary>
        public String WifiCellString  = null;
        /// <summary>
        /// 地理位置类型 1:GPS 定位位置  2:基站定位位置   3:WIFI+基站定位
        /// Location type 1:GPS location position 2: base station location location 3:WIFI+ base station location
        /// </summary>
        public int LbsType  = 1;
        /// <summary>
        /// 结尾封包 @E#@
        /// End packet @E#@
        /// </summary>
        public String FootCode = "@E#@";

        public C003(){}
        /// <summary>
        /// 初始化模型
        /// </summary>
        /// <param name="cmdStr">命令代码</param>
        public C003(String cmdStr)
        {
            //@B#@|01|003|111112222233333|8888888888888888|1.0.1|1|55|20160715150323 | 125.48276 | 37.615124 | 1 | @E#@
            String[] data = cmdStr.split("\\|");
            int   dataLength = data.length;
            System.out.println("dataLength = " + dataLength);
            System.out.println("data[0] = " + data[0]);
            System.out.println("data[2] = " + data[2]);
            //正确格式下 最小长度为13
            if (dataLength >= 12 && "@B#@".equals(data[0]) && FunCode.equals(data[2]))
            {
                
            	HeadCode = data[0];
                Protocol = data[1];
                System.out.println("data[3] = " + data[3]);
                IMEI = data[3];
                IMSI = data[4];
                WearVersion = data[5];
                WearState = Integer.parseInt(data[6]);
                Power = Integer.parseInt(data[7]);
                TerminalTime = data[8];
                LbsType = Integer.parseInt(data[dataLength - 2]);
                FootCode = data[dataLength - 1];
                DecimalFormat df = new DecimalFormat("#.00000");
                //GPS数据
                if (LbsType == 1)
                {
                	String lat = df.format(Float.parseFloat(data[9]));
                	String lon = df.format(Float.parseFloat(data[10]));
                    GpsString = lat + "|" + lon;
                }
                //基站数据
                if (LbsType == 2)
                {
                    CellString = "";
                    int lbsCount = data.length - 11;
                    for (int i = 0; i < lbsCount; i++)
                    {
                        if (i >= lbsCount - 1)
                        {
                            CellString += data[9 + i];
                        }
                        else
                        {
                            CellString += data[9 + i] + "|";
                        }
                    }


                }
                //Wife数据+基站数据
                if (LbsType == 3)
                {
                	CellString = data[10];
                	WifiCellString =  data[9];
                }
            }
            else {
                Protocol = null;
            }

        }

        public  String ToString()
        {
           String rString = HeadCode + "|";
            rString += Protocol + "|";
            rString += FunCode + "|";
            rString += IMEI + "|";
            rString += IMSI + "|";
            rString += WearVersion + "|";
            rString += WearState + "|";
            rString += Power + "|";
            rString += TerminalTime + "|";
            if (LbsType == 1)
            {
                rString += GpsString + "|";
            }
            if (LbsType == 2)
            {
                rString += CellString + "|";
            }
            if (LbsType == 3)
            {
                rString += WifiCellString + "|";
            }
            rString += LbsType + "|";
            rString += FootCode;
            return rString;
        }
    }




